package com.masterclient.app.modules.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.coupon.adapter.CouponPagerAdapter;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.bean.order.ConfirmOrderCoupons;
import com.eoner.baselibrary.widget.EnhanceTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/24 14:03
 * 功能说明：
 */

public class CouponChooseActivity extends BaseActivity {

    @BindView(R.id.tabLayout)
    EnhanceTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private List<Fragment> fragmentList;
    private CouponPagerAdapter pagerAdapter;
    private List<CouponMessage> list = new ArrayList<>();
    private List<CouponMessage> inValidList = new ArrayList<>();
    ConfirmOrderCoupons data;
    Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_choose);
        ButterKnife.bind(this);

        intent = getIntent();
        Bundle bundle = getIntent().getExtras();

        data = (ConfirmOrderCoupons) bundle.getSerializable("cards");
        if (data != null) {
            if (data.getValids() != null) {
                for (CouponMessage bean : data.getValids()) {
                    bean.setValid(true);
                    list.add(bean);
                }
            }
            if (data.getInvalids() != null) {
                for (CouponMessage bean : data.getInvalids()) {
                    bean.setValid(false);
                    inValidList.add(bean);
                }
            }
        }

        fragmentList = new ArrayList<>();
        fragmentList.add(CouponChooseFragment.getInstance(CouponChooseFragment.UNUSE,list));
        fragmentList.add(CouponChooseFragment.getInstance(CouponChooseFragment.USELESS,inValidList));

        String[] sTitle = {"可用券("+list.size()+")", "不可用券("+inValidList.size()+")"};
        pagerAdapter = new CouponPagerAdapter(getSupportFragmentManager(), fragmentList, sTitle);
        for (int i = 0; i < sTitle.length; i++) {
            tabLayout.addTab(sTitle[i], i == 0);
        }
        viewPager.setAdapter(pagerAdapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);
        (tabLayout.getTabLayout()).getTabAt(bundle.getInt("position")).select();
    }

    @OnClick(R.id.view_back)
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_back:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
